package dao;

import entity.Admin;
import entity.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements IUserDao {
    private static Map<String, User> userMap = new HashMap<>();

    static {
        Admin admin = new Admin("U000001", "admin", "ynuinfo#777", new Date(), "12345678901");
        userMap.put(admin.getUserId(), admin);
    }

    @Override
    public boolean addUser(User user) {
        if (userMap.containsKey(user.getUserId())) {
            return false;
        }
        userMap.put(user.getUserId(), user);
        return true;
    }

    @Override
    public boolean deleteUser(String userId) {
        if (!userMap.containsKey(userId)) {
            return false;
        }
        if (userId.equals("U000001")) {
            return false;
        }
        userMap.remove(userId);
        return true;
    }

    @Override
    public boolean updateUser(User user) {
        if (!userMap.containsKey(user.getUserId())) {
            return false;
        }
        userMap.put(user.getUserId(), user);
        return true;
    }

    @Override
    public User getUserById(String userId) {
        return userMap.get(userId);
    }

    @Override
    public User getUserByUsername(String username) {
        for (User user : userMap.values()) {
            if (user.getUsername().equals(username)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> getAllUsers() {
        return new ArrayList<>(userMap.values());
    }

    @Override
    public User login(String username, String password) {
        User user = getUserByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }
}